Jan-Philipp Kolb
8 Mai 2017
Modularer Aufbau
Import
Wie wird das Github Verzeichnis genutzt?
https://github.com/Japhilko/RInterfaces
Es lohnt sich immer wieder zu dieser Seite zurückzukehren, weil hier alle relevanten Dokumente verlinkt sind.
Grundsätzlich kann man der Veranstalung am Besten mit dem kompletten File folgen. Wenn Teile heruntergeladen werden sollen, bietet es sich an, das entsprechende pdf herunterzuladen.
Zum Ausdrucken eignen sich die pdf-Dateien besser.
Diese können mit dem Raw Button heruntergeladen werden.
Raw Button zum Download
Begleitend zu den Folien wird meistens auch ein R-File angeboten.
Hier können Sie entweder das gesamte R-File herunterladen und in R ausführen oder einzelne Befehle per Copy/Paste übernehmen.
Vereinzelt sind auch Datensätze vorhanden.
.csv Dateien können direkt von R eingelesen werden (wie das geht werde ich noch zeigen).
Wenn die .csv Dateien heruntergeladen werden sollen auch den Raw Button verwenden.
Alle anderen Dateien (bspw. .RData) auch mittels Raw Button herunterladen.
Gehen Sie auf https://cran.r-project.org/ und suchen Sie in dem Bereich, wo die Pakete vorgestellt werden, nach Paketen,…
R unterstützt von Haus aus schon einige wichtige Formate:
read.csv()read.fwf()read.delim()Import Button
https://data.montgomerycountymd.gov/api/views/6rqk-pdub/rows.csv?accessType=DOWNLOAD
So findet man heraus, in welchem Verzeichnis man sich gerade befindet
getwd()So kann man das Arbeitsverzeichnis ändern:
Man erzeugt ein Objekt in dem man den Pfad abspeichert:
main.path <- "C:/" # Beispiel für Windows
main.path <- "/users/Name/" # Beispiel für Mac
main.path <- "/home/user/" # Beispiel für LinuxUnd ändert dann den Pfad mit setwd()
setwd(main.path)Bei Windows ist es wichtig Slashs anstelle von Backslashs zu verwenden.
library(readr) ist für den Import von fremden Datenformaten hilfreichforeign einlesen.library(readr)
rows <- read_csv("https://data.montgomerycountymd.gov/api/views/6rqk-pdub/rows.csv?accessType=DOWNLOAD").csv-Daten aus dem Web importieren - zweites Beispielurl <- "https://raw.githubusercontent.com/Japhilko/
GeoData/master/2015/data/whcSites.csv"
whcSites <- read.csv(url) head(data.frame(whcSites$name_en,whcSites$category))## whcSites.name_en
## 1 Cultural Landscape and Archaeological Remains of the Bamiyan Valley
## 2 Minaret and Archaeological Remains of Jam
## 3 Historic Centres of Berat and Gjirokastra
## 4 Butrint
## 5 Al Qal'a of Beni Hammad
## 6 M'Zab Valley
## whcSites.category
## 1 Cultural
## 2 Cultural
## 3 Cultural
## 4 Cultural
## 5 Cultural
## 6 Cultural
install.packages("haven")library(haven)
mtcars <- read_sav("https://github.com/Japhilko/RInterfaces/raw/master/data/mtcars.sav")library(haven)
oecd <- read_dta("https://github.com/Japhilko/IntroR/raw/master/2017/data/oecd.dta")read.X() Funktionen stehen viele write.X() Funktionen zur Verfügung.RData)A <- c(1,2,3,4)
B <- c("A","B","C","D")
mydata <- data.frame(A,B).RData Format am Besten:save(mydata, file="mydata.RData").csv Format abspeichernwrite.csv(mydata,file="mydata.csv") write.csv2 besserwrite.csv2(mydata,file="mydata.csv") library(foreign)
write.dta(mydata,file="data/mydata.dta") rioinstall.packages("rio")library("rio")
# create file to convert
export(mtcars, "data/mtcars.sav")export(mtcars, "data/mtcars.dta")
# convert Stata to SPSS
convert("data/mtcars.dta", "data/mtcars.sav")Quick R für das Exportieren von Daten:
Hilfe zum Export auf dem cran Server
xlsxlibrary("xlsx")
dat <- read.xlsx("cult_emp_sex.xls",1)fileXls <- "data/newFile.xlsx"
unlink(fileXls, recursive = FALSE, force = FALSE)
exc <- loadWorkbook(fileXls, create = TRUE)
createSheet(exc,'Input')
saveWorkbook(exc)input <- data.frame('inputType'=c('Day','Month'),'inputValue'=c(2,5))
writeWorksheet(exc, input, sheet = "input", startRow = 1, startCol = 2)
saveWorkbook(exc)myFunction <- function(){
aa <- rnorm(200)
bb <- rnorm(200)
res <- lm(aa~bb)$res
return(res)
}Markdown ist eine sehr einfache Syntax, die es Benutzern erlaubt, aus einfachen Textdateien gut gelayoutete Dokumente zu erstellen.
**fettes Beispiel**
*kursives Beispiel*
~~durchgestrichen~~
- Aufzählungspunkt
fettes Beispiel
kursives Beispiel
durchgestrichen
### Überschrift Ebene 3
#### Überschrift Ebene 4
[Meine Github Seite](https://github.com/Japhilko)


n=100Ein inline Codeblock: 100
| Argument | Beschreibung |
|---|---|
| eval | Soll Rcode evaluiert werden? |
| warning | Sollen Warnings angezeigt werden? |
| cache | Soll der Output gespeichert werden? |
knitrinstall.packages("knitr")library("knitr")kable um Tabellen zu erzeugenkable erzeugena <- runif(10)
b <- rnorm(10)
ab <- cbind(a,b)
kable(ab)| a | b |
|---|---|
| 0.4595360 | -1.0480982 |
| 0.1212409 | 0.5099393 |
| 0.9461977 | 0.1667781 |
| 0.4661135 | 0.2633298 |
| 0.3865641 | -0.3851828 |
| 0.8202662 | -0.7292391 |
| 0.9891585 | -0.3318638 |
| 0.0486718 | -0.8682303 |
| 0.2026470 | -0.9199542 |
| 0.9157137 | 1.0837656 |
cache=T angegeben ist, wird das Ergebnis des Chunks abgespeichert---
title: "Intro - Erste Schritte"
author: "Jan-Philipp Kolb"
date: "10 April 2017"
output:
beamer_presentation:
colortheme: beaver
theme: CambridgeUS
---
output:
beamer_presentation:
toc: yes
\Sexpr{}
citation() bekommt man sehr schnell die Referenzinstall.packages("RMySQL")citation("RMySQL")##
## To cite package 'RMySQL' in publications use:
##
## Jeroen Ooms, David James, Saikat DebRoy, Hadley Wickham and
## Jeffrey Horner (2017). RMySQL: Database Interface and 'MySQL'
## Driver for R. R package version 0.10.11.
## https://CRAN.R-project.org/package=RMySQL
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {RMySQL: Database Interface and 'MySQL' Driver for R},
## author = {Jeroen Ooms and David James and Saikat DebRoy and Hadley Wickham and Jeffrey Horner},
## year = {2017},
## note = {R package version 0.10.11},
## url = {https://CRAN.R-project.org/package=RMySQL},
## }
---
title: "R Schnittstellen"
author: "Jan-Philipp Kolb"
date: "21 April 2017"
output:
pdf_document: default
bibliography: Rschnittstellen.bib
---
date()## [1] "Sat May 06 00:36:37 2017"
$$
\begin{equation}\label{eq2}
t_{i}=\sum\limits_{k=1}^{M_{i}}{y_{ik}}=M_{i}\bar{Y}_{i}.
\end{equation}
$$
Folie mit zwei Spalten
====================================
Erste Spalte
***
Zweite Spalte
transition: rotate
Ein neues Kapitel einfügen
====================================
type: section
Anderer Folientyp
====================================
type: prompt
Noch ein anderer Folientyp
====================================
type: alert
Meine Präsentation
========================================
author: Jan-Philipp Kolb
font-family: 'Impact'
Meine Präsentation
========================================
author: Jan-Philipp Kolb
font-import: http://fonts.googleapis.com/css?family=Risque
font-family: 'Risque'
Normale Schriftgröße
<small>This sentence will appear smaller.</small>

---
title: "ioslides Beispiel"
author: "Jan-Philipp Kolb"
date: "20 April 2017"
output:
ioslides_presentation:
logo: figure/Rlogo.png
---
library(knitr)
a <- data.frame(a=1:10,b=10:1)
kable(table(a))| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
knitr EnginesUm den Präsentationstyp zu ändern kann man das CSS verändern
install.packages("rticles")Das Paket rmdformats - HTML Output Formats and Templates for ‘rmarkdown’
install.packages("rmdformats")ProjectTemplate - Automates the Creation of New Statistical Analysisinstall.packages("ProjectTemplate")tufte - Tufte’s Styles for R Markdown Documentsinstall.packages("tufte")import sys
print(sys.version)## 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]
cmd in Suche eingibt.jupyter notebook
url <- "https://raw.githubusercontent.com/Japhilko/
GeoData/master/2015/data/whcSites.csv"
whcSites <- read.csv(url) whcSitesDat <- with(whcSites,data.frame(name_en,
category))library(knitr)
kable(head(whcSitesDat))| name_en | category |
|---|---|
| Cultural Landscape and Archaeological Remains of the Bamiyan Valley | Cultural |
| Minaret and Archaeological Remains of Jam | Cultural |
| Historic Centres of Berat and Gjirokastra | Cultural |
| Butrint | Cultural |
| Al Qal’a of Beni Hammad | Cultural |
| M’Zab Valley | Cultural |
whcSitesDat2 <- with(whcSites,data.frame(name_en,category,longitude,latitude,date_inscribed,area_hectares,danger_list))datatable kann man eine erste interaktive Tabelle erstellen:library('DT')
datatable(whcSitesDat2)magrittrinstall.packages("magrittr")library("magrittr")library(magrittr)
str1 <- "Hallo Welt"
str1 %>% substr(1,5)## [1] "Hallo"
str1 %>% substr(1,5) %>% toupper()## [1] "HALLO"
leafletinstall.packages("leaflet")library("leaflet")m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=whcSites$lon,
lat=whcSites$lat,
popup=whcSites$name_en)
mwhcSites$color <- "red"
whcSites$color[whcSites$category=="Cultural"] <- "blue"
whcSites$color[whcSites$category=="Mixed"] <- "orange"m1 <- leaflet() %>%
addTiles() %>%
addCircles(lng=whcSites$lon,
lat=whcSites$lat,
popup=whcSites$name_en,
color=whcSites$color)Weltkulturerbe
m2 <- leaflet() %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles("Stamen.Toner", group = "Toner") %>%
addProviderTiles("Stamen.TonerLite", group = "Toner Lite") %>%
addCircles(lng=whcSites$lon,
lat=whcSites$lat,
popup=whcSites$name_en) %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
options = layersControlOptions(collapsed = FALSE)
)
m2outline <- quakes[chull(quakes$long, quakes$lat),]map <- leaflet(quakes) %>%
# Base groups
addTiles(group = "OSM (default)") %>%
addProviderTiles("Stamen.Toner", group = "Toner") %>%
addProviderTiles("Stamen.TonerLite", group = "Toner Lite") %>%
# Overlay groups
addCircles(~long, ~lat, ~10^mag/5, stroke = F, group = "Quakes") %>%
addPolygons(data = outline, lng = ~long, lat = ~lat,
fill = F, weight = 2, color = "#FFFFCC", group = "Outline") %>%
# Layers control
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = c("Quakes", "Outline"),
options = layersControlOptions(collapsed = FALSE)
)
maplibrary(sp)
Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
Sr3 = Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5)))
Sr4 = Polygon(cbind(c(5, 6, 6, 5, 5), c(4, 4, 3, 3, 4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr4, Sr3), "s3/4")
SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3)
leaflet(height = "300px") %>% addPolygons(data = SpP)library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)m <- leaflet() %>% setView(lng = -71.0589, lat = 42.3601, zoom = 12)
m %>% addTiles()m %>% addProviderTiles("Stamen.Toner")m %>% addProviderTiles("CartoDB.Positron")m %>% addProviderTiles("Esri.NatGeoWorldMap")m %>% addProviderTiles("OpenTopoMap")m %>% addProviderTiles("Thunderforest.OpenCycleMap")leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 4) %>%
addWMSTiles(
"http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers = "nexrad-n0r-900913",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "Weather data © 2012 IEM Nexrad"
)m %>% addProviderTiles("MtbMap") %>%
addProviderTiles("Stamen.TonerLines",
options = providerTileOptions(opacity = 0.35)) %>%
addProviderTiles("Stamen.TonerLabels")greenLeafIcon <- makeIcon(
iconUrl = "http://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
leaflet(data = quakes[1:4,]) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = greenLeafIcon)leaflet(quakes) %>% addTiles() %>% addMarkers(
clusterOptions = markerClusterOptions()
)leaflet() %>% addTiles() %>%
addRectangles(
lng1=-118.456554, lat1=34.078039,
lng2=-118.436383, lat2=34.062717,
fillColor = "transparent"
)